package com.example.arithmeticleetcode.leetcode.jan;


import java.util.HashMap;
import java.util.Map;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-13 09:28
 **/
public class Demo10 {

    public static void main(String[] args) {
        int[] nums1 = {43024, 99908};
        int[] nums2 = {1864};
        new Demo10().numTriplets(nums1, nums2);

        int[] nums11 = {7, 4};
        int[] nums21 = {5, 2, 8, 9};
        new Demo10().numTriplets(nums11, nums21);
    }

    public int numTriplets(int[] nums1, int[] nums2) {
        Map<Long, Integer> map1 = new HashMap<>();
        Map<Long, Integer> map2 = new HashMap<>();
        getNumsMulty(nums1, map1);
        getNumsMulty(nums2, map2);
        int count = 0;
        for (int i : nums1) {
            long temp = (long) i * i;
            if (!map2.containsKey(temp)) continue;
            count += map2.get(temp);
        }
        for (int i : nums2) {
            long temp = (long) i * i;
            if (!map1.containsKey(temp)) continue;
            count += map1.get(temp);
        }
        return count;
    }

    private void getNumsMulty(int[] nums, Map<Long, Integer> map) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                long multy = (long) nums[i] * nums[j];
                map.put(multy, map.getOrDefault(multy, 0) + 1);
            }
        }
    }

    /**
     * 1578. 避免重复字母的最小删除成本
     *
     * @param s
     * @param cost
     * @return
     */
    public int minCost(String s, int[] cost) {
        if (cost.length == 1) return 0;
        char[] chars = s.toCharArray();
        int costs = 0;
        int preIndex = 0;
        for (int i = 1; i < chars.length; i++) {
            if (chars[preIndex] == chars[i]) {
                if (cost[preIndex] > cost[i]) {
                    costs += cost[i];
                    continue;
                } else {
                    costs += cost[preIndex];
                    preIndex = i;
                }
            } else {
                preIndex = i;
            }
        }
        return costs;
    }
}
